package com.codeoss.framework.view.renderer;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.Table;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.model.SelectItem;

@Service
public class TableRenderer extends CommonComponentRenderer {
	@Autowired
	private IConverterStore converterStore;
	@Autowired
	private ITableStore tableStore;
	
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		Table table=(Table)component;
		viewSerializeMap.put("editable", table.isEditable());
		viewSerializeMap.put("halfEditable", table.isHalfEditable());
		viewSerializeMap.put("pageable", table.isPageable());
		viewSerializeMap.put("pageSize", table.getPageSize());
		viewSerializeMap.put("multiSelect", table.isMultiSelect());
		String dataSource=table.getDataSource();
		if(!StringUtils.isEmpty(dataSource)) {
			DataSourceConfig dataSourceConfig=yunContext.getPage().getDataSourceConfigById(dataSource);
			com.codeoss.framework.data.metadata.Table ttable=tableStore.get(dataSourceConfig.getTableName());
			viewSerializeMap.put("keyColumnName", ttable.getPrimaryKeyColumn().getColumnName());
		}
		if(table.isHalfEditable()) {
			viewSerializeMap.put("formViewPageCode", table.getFormViewPageCode());
		}
		
	}
	
	protected PropSerializeMap startRenderProp(YunContext yunContext,
			Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap=super.startRenderProp(yunContext, component, viewPropMap);
		Table table=(Table)component;
		if(table.isEditable()) {
			List<Map<String,PropSerializeMap>> data=yunContext.getAttribute(table.getId()+"_data");
			propSerializeMap.put("data", data);
		}else {
			TableData tableData=(TableData)yunContext.getAttribute(component.getId()+"_tabledata");
			Map<String,List<SelectItem>> selectItemMap=yunContext.getAttribute(component.getId()+"_selectItemMap");
			if(tableData==null) {
				return propSerializeMap;
			}
			Page page=yunContext.getPage();
			DataSourceConfig dataSourceConfig=page.getDataSourceConfigById(table.getDataSource());
			com.codeoss.framework.data.metadata.Table ttable=tableStore.get(dataSourceConfig.getTableName());
			IConverter datetimeConverter=converterStore.getConverter("datetime");
			IConverter dateConverter=converterStore.getConverter("date");
			IConverter timeConverter=converterStore.getConverter("time");
			IConverter booleanConverter=converterStore.getConverter("boolean");
			List<Map<String,Object>> dataList=tableData.getData();
			for(int i=0;i<dataList.size();i++) {
				Map<String,Object> map=dataList.get(i);
				Iterator<Map.Entry<String, Object>> it=map.entrySet().iterator();
				while(it.hasNext()) {
					Map.Entry<String, Object> entry=it.next();
					Object value=entry.getValue();
					if(value!=null) {
						if(value instanceof Timestamp) {
							String sValue=datetimeConverter.convertObjectToString(value);
							map.put(entry.getKey(), sValue);
						}else if(value instanceof Date) {
							String sValue=dateConverter.convertObjectToString(value);
							map.put(entry.getKey(), sValue);
						}else if(value instanceof Time) {
							String sValue=timeConverter.convertObjectToString(value);
							map.put(entry.getKey(), sValue);
						}else {
							String columnName=entry.getKey();
							Column column=ttable.getColumnByColumnName(columnName);
							if(column.getDataType().equals("boolean") && !(value instanceof Boolean)) {
								map.put(entry.getKey(), booleanConverter.convertStringToObject((String)value));
							}
						}
					}
				}
			}
			propSerializeMap.put("tableData", tableData);
			propSerializeMap.put("selectItemMap", selectItemMap);
			
			if(table.isHalfEditable()) {
				Object idValue=yunContext.getCurrentId();
				propSerializeMap.put("currentId", idValue);	
			}
		}
		
		return propSerializeMap;
	}
}
